package com.weixin.paleituo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.bean.PtListBean;
import com.weixin.paleituo.eventbus.SharedTemplateClickEvent;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.RoundCircleTransform;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 共享家样板间瀑布流列表Adapter
 *
 * @author fengzhen
 * @version v1.0, 2018/4/16
 */
public class TemplateRecyclerAdapter extends RecyclerView.Adapter {
    private List<PtListBean> mDataList;
    private Context mContext;
    private Boolean mClickable;

    public TemplateRecyclerAdapter(List<PtListBean> dataList, boolean clickable) {
        this.mDataList = dataList;
        this.mClickable = clickable;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View inflate = UIUtils.inflate(parent.getContext(), R.layout.item_template_house, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        final PtListBean bean = mDataList.get(position);
        Glide.with(mContext)
                .load(bean.getSurfaceUrl())
                .asBitmap()
                .error(R.mipmap.buffer)
                .transform(new RoundCircleTransform(mContext, 2))
                .placeholder(R.mipmap.buffer)
                .into(viewHolder.mImgItemTemplateHouse);
        viewHolder.mTvTitleItemTemplateHouse.setText(bean.getName());
        viewHolder.mTvNumItemTemplateHouse.setText(
                String.format(mContext.getString(R.string.string_person_num), bean.getNop())
        );
        if (mClickable) {
            viewHolder.mRelative.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(new SharedTemplateClickEvent(viewHolder.getAdapterPosition() - 1, bean.getId()));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_item_template_house)
        ImageView mImgItemTemplateHouse;
        @BindView(R.id.tv_title_item_template_house)
        TextView mTvTitleItemTemplateHouse;
        @BindView(R.id.tv_num_item_template_house)
        TextView mTvNumItemTemplateHouse;
        @BindView(R.id.relative_item_template_house)
        RelativeLayout mRelative;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
